package scheduler.fixtures;

import fitlibrary.SetUpFixture;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import se392.ateam2006.meetingmgmt.MeetingMgmtRemote;
import se392.ateam2006.meetingmgmt.meeting.Duration;
import se392.ateam2006.resourcemgmt.ResourceMgmtRemote;
import se392.ateam2006.resourcemgmt.room.RoomEntity;
import se392.ateam2006.user.UserEntity;
import se392.ateam2006.user.UserMgmtRemote;

/**
 *
 * @author James
 */
public class CreateMeeting extends SetUpFixture {

    private MeetingMgmtRemote meetings;
    private ResourceMgmtRemote resources;
    private UserMgmtRemote users;

    /** Creates a new instance of CreateMeeting */
    public CreateMeeting(MeetingMgmtRemote meetings, ResourceMgmtRemote resources, UserMgmtRemote users) {
        this.meetings = meetings;
        this.resources = resources;
        this.users = users;
    }

    public void initiatorIdStartDateRangeEndDateRangeDurationTitleDescriptionVenueSize(String initiator, String id, String startDateRange,
            String endDateRange, String duration, String title, String description, String venue, int size) throws Exception {
        UserEntity initiator_obj = users.createUser(initiator);
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd kk:mm");
        GregorianCalendar startDateRangeCal = new GregorianCalendar();
        GregorianCalendar endDateRangeCal = new GregorianCalendar();
        startDateRangeCal.setTime(format.parse(startDateRange));
        endDateRangeCal.setTime(format.parse(endDateRange));

        if (startDateRangeCal.getTime().getHours() == 24) {
            System.err.println("PROBLEM WITH DATES: Text: " + startDateRange + ", date: " + startDateRangeCal.getTime().toString());
        }

        int hours;
        int minutes;
        int days;
        String[] parts = duration.split(" ");
        days = Integer.parseInt(parts[0]);
        parts = parts[1].split(":");
        hours = Integer.parseInt(parts[0]);
        minutes = Integer.parseInt(parts[1]);

        Duration duration_obj = meetings.createDuration(days, hours, minutes);
        int roomSize = 30;
        String equipment = "Projector";

        RoomEntity room = null;
        if (!venue.equals("blank")) {
            if ((room = resources.findRoomByID(venue)) == null) {
                throw new Exception();
            }
        }

        if (description.equals("blank")) {
            description = "";
        }

        meetings.createMeeting(initiator_obj, id, title, description, startDateRangeCal, endDateRangeCal, duration_obj, size, equipment, room);

    }
}
